//
//  AppDelegate.m
//  JobFreely
//
//  Created by Dale on 2017/5/23.
//  Copyright © 2017年 Dale. All rights reserved.
//

#import "AppDelegate.h"
#import "WXApi.h"
#import "WXApiManager.h"
#import "UIViewController+MMDrawerController.h"
#import "LeftViewController.h"
#import "RightViewController.h"
#import "CenterViewController.h"
#import "MMDrawerVisualState.h"
#import "BaseNavigationViewController.h"
#import "JFFeedBackViewController.h"
#import "JFScrumMainViewController.h"
#import "JFSettingViewController.h"
#import "JFFriendMainViewController.h"
#import "JFLoginViewController.h"
#import "MessageManager.h"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self initialMethods];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
#if isTest
    JFUser *user = [JFUser mainJFUser];
    user.user_id = @"1102c20650a911e7af3b1dd9e90559b2";
    user.user_name = @"Dale";
    self.window.rootViewController = [self createHomeVC];
#else
    self.window.rootViewController = [[JFLoginViewController alloc] initWithNibName:@"JFLoginViewController" bundle:nil];
#endif
    [self.window makeKeyAndVisible];
    return YES;
}

- (MMDrawerController *)createHomeVC {
    LeftViewController *leftVC = [[LeftViewController alloc] initWithNibName:@"LeftViewController" bundle:nil];
    
    CenterViewController *centerVC = [[CenterViewController alloc] init];
    
    RightViewController *rightVC = [[RightViewController alloc] init];
    
    MMDrawerController *drawerVC = [[MMDrawerController alloc] initWithCenterViewController:centerVC leftDrawerViewController:leftVC rightDrawerViewController:rightVC];
    drawerVC.openDrawerGestureModeMask = MMOpenDrawerGestureModeCustom;
    drawerVC.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
    drawerVC.showsShadow = YES;
    drawerVC.maximumLeftDrawerWidth = 80;
    drawerVC.maximumRightDrawerWidth = 80;
//    [drawerVC bouncePreviewForDrawerSide:MMDrawerSideLeft completion:nil];
//    [drawerVC bouncePreviewForDrawerSide:MMDrawerSideLeft distance:0 completion:nil];
    [drawerVC setDrawerVisualStateBlock:[MMDrawerVisualState slideVisualStateBlock]];
    //自定义手势
    [drawerVC setGestureShouldRecognizeTouchBlock:^BOOL(MMDrawerController *drawerController, UIGestureRecognizer *gesture, UITouch *touch) {
        BOOL shouldRecognizeTouch = NO;
        if(drawerController.openSide == MMDrawerSideNone &&
           [gesture isKindOfClass:[UIPanGestureRecognizer class]]){
            CenterViewController *centerVC = (CenterViewController *)drawerController.centerViewController;
            BaseNavigationViewController *baseNav = centerVC.childViewControllers[centerVC.curIndex];
            //判断哪个控制器可以滑到抽屉
            if([baseNav.topViewController isKindOfClass:[JFSettingViewController class]]||
               [baseNav.topViewController isKindOfClass:[JFScrumMainViewController class]] ||
               [baseNav.topViewController isKindOfClass:[JFFriendMainViewController class]] ||
               [baseNav.topViewController isKindOfClass:[JFFeedBackViewController class]]){
                shouldRecognizeTouch = YES;//返回yes表示可以滑动到左右侧抽屉
            }
        }
        return shouldRecognizeTouch;
    }];

    return drawerVC;
}

//MARK: iOS9 before
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return  [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    NSLog(@"sourceApplication: %@", sourceApplication);
    NSLog(@"URL scheme:%@", [url scheme]);
    NSLog(@"URL query: %@", [url query]);
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}

//MARK: iOS9 After
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
    NSLog(@"Calling Application Bundle ID: %@", options[UIApplicationOpenURLOptionsSourceApplicationKey]);
    NSLog(@"URL scheme:%@", [url scheme]);
    NSLog(@"URL query: %@", [url query]);
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}

- (void)initialMethods {
    
    //推送相关
    [MessageManager registerUserNotification];
    [[MessageManager sharedInstance] startGeTui];
    //配置网络
    [NetworkManager configureNetwork];
    //向微信注册
    [WXApi registerApp:WX_AppID withDescription:@"JobFree1.0"];
    //向微信注册支持的文件类型
    UInt64 typeFlag = MMAPP_SUPPORT_TEXT | MMAPP_SUPPORT_PICTURE | MMAPP_SUPPORT_LOCATION | MMAPP_SUPPORT_VIDEO |MMAPP_SUPPORT_AUDIO | MMAPP_SUPPORT_WEBPAGE | MMAPP_SUPPORT_DOC | MMAPP_SUPPORT_DOCX | MMAPP_SUPPORT_PPT | MMAPP_SUPPORT_PPTX | MMAPP_SUPPORT_XLS | MMAPP_SUPPORT_XLSX | MMAPP_SUPPORT_PDF;
    [WXApi registerAppSupportContentFlag:typeFlag];
}


- (void)applicationWillResignActive:(UIApplication *)application {
   
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
   
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
   
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
  }


- (void)applicationWillTerminate:(UIApplication *)application {
    
}


#pragma mark - 用户通知(推送)回调 _IOS 8.0以上使用
/** 已登记用户通知 */
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    // 注册远程通知（推送）
    [application registerForRemoteNotifications];
}

#pragma mark - 远程通知(推送)回调
/** 远程通知注册成功委托 */
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"\n>>>[DeviceToken Success]:%@\n\n", token);
    UserDefaultsSave(token, @"CurrentDeviceToken");
    [[MessageManager sharedInstance] registerDeviceToken:token];
}

/** 远程通知注册失败委托 */
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"\n>>>[DeviceToken Error]:%@\n\n", error.description);
}

#pragma mark - APP运行中接收到通知(推送)处理
/** APP已经接收到“远程”通知(推送) - (App运行在后台/App运行在前台) */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@"\n>>>[Receive RemoteNotification]:%@\n\n", userInfo);
}

/** APP已经接收到“远程”通知(推送) - 透传推送消息  */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
    // 处理APN
    NSLog(@"\n>>>[Receive RemoteNotification - Background Fetch]:%@\n\n", userInfo);
    completionHandler(UIBackgroundFetchResultNewData);
}



@end
